package com.wuxu.ads_no.keepalive;

import android.content.Context;

/**
 * <pre>
 * Created by DengDongQi on 2020/8/5
 * 自定义异常处理
 * </pre>
 */
public class CrashHandler implements Thread.UncaughtExceptionHandler {

    private static CrashHandler presenter = null;
    private Thread.UncaughtExceptionHandler mDefaultCrashHandler = null;
    private Context mContext = null;

    public static CrashHandler getInstance(){
        if(presenter == null){
            synchronized (CrashHandler.class){
                if(presenter == null){
                    presenter =new CrashHandler();
                }
            }
        }
        return presenter;
    }

    private CrashHandler(){

    }

    public void init(Context context){
        //获取系统默认的异常处理器
        mDefaultCrashHandler = Thread.getDefaultUncaughtExceptionHandler();
        //将当前实例设为系统默认的异常处理器
        Thread.setDefaultUncaughtExceptionHandler(this);
        mContext = context.getApplicationContext();
    }

    @Override
    public void uncaughtException(Thread thread, Throwable throwable) {
        //打印出当前调用栈信息
        throwable.printStackTrace();
        //交给系统的UncaughtExceptionHandler处理
        //mDefaultCrashHandler!!.uncaughtException(thread, throwable)

        //android.os.Process.killProcess(android.os.Process.myPid())

    }
}
